<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button id="btn">按我</button>
</body>
<script>
class MacroCommand {
  constructor() {
    this.commandsList = []
  }
  add(child) {
    this.commandsList.push(child)
  }
  execute() {
    this.commandsList.map(command => command.execute())
  }
}
let openTvCommand = {
  execute() {
    console.log('打开电视');
  },
  add() {
    throw new Error('叶对象不能添加子节点')
  }
}
let macroCommand = new MacroCommand()
macroCommand.add(openTvCommand)
openTvCommand.add(macroCommand)


class Component {
  constructor() {
    this.commandsList = []
  }
}
class Composite extends Component {
  constructor() {
    super()
  }
  add(child) {
    this.commandsList.push(child)
  }
}
class Leaf extends Component {
  constructor() {
    super()
  }
  add(child) {
    throw new Error('叶对象不能再添加子节点')
  }
}
let root = new Composite()
let c1 = new Composite()
let c2 = new Composite()

let leaf1 = new Leaf()
let leaf2 = new Leaf()
function init() {
  root.add(c1)
  root.add(c2)
  c1.add(leaf1)
  c2.add(leaf2)
  console.log(root);
}
init()
</script>
</html>